package com.syxp.pda.action.plan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.CHKLine;
import com.syxp.pda.service.plan.CHKLineService;

public class CHKLineAction extends BaseAction {
	@Resource
	private CHKLineService chklineService;
	
	private int lineID;
	private CHKLine chkLine;
	private String isCHKOrder;
	private int start ;
	
	private int limit ;
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String load() {
		List<CHKLine> lines=null;
		JsonArray jarray=new JsonArray();
		QueryFilter filter = new QueryFilter() ;
		filter.setPagingBean(new PagingBean(start,limit)) ;
		if(limit==0){  //为了下拉框做一个判断
			lines = chklineService.getDao().getAll();
		}else{
			lines = chklineService.getDao().getAll(filter);
		}
		JsonObject result = new JsonObject() ;
		try {
			
			for(CHKLine o: lines) {
				JsonObject jobj=new JsonObject();
				jobj.addProperty("lineName", o.getLineName());
				jobj.addProperty("isCHKOrder", o.getIsCHKOrder()==1?"是":"否");
				jobj.addProperty("lineDesc", o.getLineDesc());
				jobj.addProperty("professName", o.getProfess().getProfessName());
				jobj.addProperty("lineID", o.getChkLineID());
				jobj.addProperty("professID", o.getProfess().getProfessID());
				jarray.add(jobj);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		if(limit==0){
			this.jsonString = gson.toJson(jarray) ;
		}else{
			result.addProperty("totalProperty",chklineService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString = gson.toJson(result) ;
		}
		
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	
	public String save() {
		try {
			System.out.println("save:"+this.chkLine.getChkLineID()+"chkLine.getIsCHKOrder():"+this.chkLine.getIsCHKOrder());
			this.chklineService.addService(chkLine);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			this.chklineService.removeService(this.lineID);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String update() {
		try {
			this.chklineService.updateService(this.chkLine);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	/**
	 * @return the chklineService
	 */
	public CHKLineService getChklineService() {
		return chklineService;
	}
	/**
	 * @param chklineService the chklineService to set
	 */
	public void setChklineService(CHKLineService chklineService) {
		this.chklineService = chklineService;
	}

	/**
	 * @return the lineID
	 */
	public int getLineID() {
		return lineID;
	}

	/**
	 * @param lineID the lineID to set
	 */
	public void setLineID(int lineID) {
		this.lineID = lineID;
	}

	/**
	 * @return the chkLine
	 */
	public CHKLine getChkLine() {
		return chkLine;
	}

	/**
	 * @param chkLine the chkLine to set
	 */
	public void setChkLine(CHKLine chkLine) {
		this.chkLine = chkLine;
	}

	/**
	 * @return the isCHKOrder
	 */
	public String getIsCHKOrder() {
		return isCHKOrder;
	}

	/**
	 * @param isCHKOrder the isCHKOrder to set
	 */
	public void setIsCHKOrder(String isCHKOrder) {
		this.isCHKOrder = isCHKOrder;
	}

}
